import 'package:flutter/material.dart';

class SliverDraggerWrapper extends StatefulWidget {
  final WidgetBuilder builder;

  final double height;

  const SliverDraggerWrapper({
    Key key,
    this.builder,
    this.height = 300,
  }) : super(key: key);

  @override
  _SliverDraggerWrapperState createState() => _SliverDraggerWrapperState();
}

class _SliverDraggerWrapperState extends State<SliverDraggerWrapper> {
  ScrollController controller;

  @override
  void initState() {
    super.initState();
    controller = ScrollController(
      initialScrollOffset: widget.height,
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final sliverList = widget.builder(context);
    return Material(
      color: Colors.transparent,
      child: Container(
        height: widget.height,
        child: NotificationListener<ScrollNotification>(
          onNotification: _onScroll,
          child: CustomScrollView(
            controller: controller,
            slivers: <Widget>[
              TransSliverHeader(
                headerHeight: widget.height,
              ),
              sliverList,
            ],
          ),
        ),
      ),
    );
  }

  bool autoJumping = false;

  bool _onScroll(ScrollNotification notification) {
    if (notification is ScrollEndNotification && !autoJumping) {
      if (controller.offset < widget.height) {
        autoJumping = true;
        controller.jumpTo(widget.height);
        autoJumping = false;
      }
    }
    return true;
  }
}

class TransSliverHeader extends StatefulWidget {
  final double headerHeight;

  const TransSliverHeader({Key key, this.headerHeight = 300}) : super(key: key);

  @override
  _TransSliverHeaderState createState() => _TransSliverHeaderState();
}

class _TransSliverHeaderState extends State<TransSliverHeader> {
  @override
  Widget build(BuildContext context) {
    return SliverPersistentHeader(
      // pinned: true,
      floating: true,
      delegate: _Delegate(
        maxExtent: widget.headerHeight,
        minExtent: 0,
      ),
    );
  }
}

class _Delegate extends SliverPersistentHeaderDelegate {
  final double maxExtent;
  final double minExtent;

  _Delegate({
    this.minExtent,
    this.maxExtent,
  });

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return Container();
  }

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate oldDelegate) {
    return true;
  }
}
